import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//查询操作
public class SelBooks {
    public static void main(String[] args) throws SQLException {
        //1.获得数据源
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        //2.获得连接
        Connection connection=(Connection)dataSource.getConnection();
        //3.获得执行器
        String sql="select * from books where id<?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,5);
        //4.执行SQL,打印查询结果
        ResultSet resultSet=statement.executeQuery();
        while(resultSet.next()){
            Books books=new Books();
            books.setId(resultSet.getInt("id"));
            books.getName(resultSet.getString("name"));
            books.setAuthor(resultSet.getString("author"));
            System.out.println(books);
        }
        //关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
